package com.tony.web;

import com.tony.bean.Member;
import com.tony.service.ArticleService;
import com.tony.service.MemberService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by Tony on 2017/1/1.
 */
@Controller
@RequestMapping("/members")
public class MemberController  {


    Log log= LogFactory.getLog(MemberController.class);

    @Autowired
    MemberService memberService;

    @Autowired
    ArticleService articleService;

    @ResponseBody
    @RequestMapping("/hello")
    public String hello(){
        System.out.println("hello world");

        return "success";
    }



    @ResponseBody
    @RequestMapping("/add")
    public String add(int id,String name){

        Member member = new Member();
        member.setId(String.valueOf(id));
        member.setNickname(name);
        memberService.add(member);

        log.info("写入成功");
        return "success";


    }

    @ResponseBody
    @RequestMapping(value = "/get")
    public  String get(int id){
        Member thisMember = memberService.get(String.valueOf(id));
        String nickName ;
        if (null != thisMember) {
            nickName = thisMember.getNickname();
        } else {
            nickName = "undifine";
        }

        log.info("nickname:" + nickName);
        return nickName;

    }


    @ResponseBody
    @RequestMapping("/del")
    public  String del(int id){
       memberService.delete(String.valueOf(id));
       log.info("delete success");
        return "success";

    }

    @ResponseBody
    @RequestMapping("/post")
    public String post(String user,String title,String link){
        articleService.postArticle(user, title, link);
        return "success";
    }

}
